import { createRouter, createWebHashHistory } from "vue-router";
import Home from "../components/Home.vue";
import Login from "../components/Login.vue";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/home",
      component: Home,
    },
    {
        path:"/",
        component:Home
    },
    {
        path:"/login",
        component:Login
    }
  ],
});

// 设置路由的全局前置首位
router.beforeEach(
    (to,from,next) => {
        console.log(`从哪里来：${from.path}，到哪里去${to.path}`)
        if (to.path == '/login') {
            next()
        } else {
            let username = window.localStorage.getItem("username")
            if (null != username) {
                next()
            } else {
                next("/login")
            }
        }
    }
)

// 设置路由的全局后置首位
router.afterEach(
    (to, from) => {
        console.log(`从哪里来：${from.path}，到哪里去${to.path}`)
    }
)

// 对外暴露路由对象
export default router